package com.ltv.xiangqibook.web;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.octo.captcha.service.CaptchaServiceException;

public class XiangQiBookCaptchaServlet extends HttpServlet {
    private static final Logger logger = LogManager.getLogger(XiangQiBookCaptchaServlet.class);

    private String sImgType = null;

    public void init( ServletConfig servletConfig ) throws ServletException {
        super.init( servletConfig );

        // For this servlet, supported image types are PNG and JPG.
        sImgType = servletConfig.getInitParameter( "ImageType" );
        sImgType = sImgType==null ? "png" : sImgType.trim().toLowerCase();
        if ( !sImgType.equalsIgnoreCase("png") && !sImgType.equalsIgnoreCase("jpg") && !sImgType.equalsIgnoreCase("jpeg") ) {
            sImgType = "png";
        }
    }

    // doGet() - handles captcha image generation.
    protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
        ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
        byte[] captchaBytes;

        if ( request.getQueryString()!=null ) {
            response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "GET request should have no query string." );
            return;
        }

        try {
            // Session ID is used to identify the particular captcha.
            String captchaId = request.getSession().getId();

            // Generate the captcha image.
            BufferedImage challengeImage = XiangQiBookCaptchaService.getInstance().getImageChallengeForID( captchaId, request.getLocale() );
            ImageIO.write( challengeImage, sImgType, imgOutputStream );
            captchaBytes = imgOutputStream.toByteArray();
        }
        catch( CaptchaServiceException cse ) {
            logger.error("CaptchaServiceException - " + cse.getMessage(), cse);
            response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Problem generating captcha image." );
            return;
        }
        catch( IOException ioe ) {
            logger.error("IOException - " + ioe.getMessage(), ioe);
            response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Problem generating captcha image." );
            return;
        }

        // Set appropriate http headers.
        response.setHeader( "Cache-Control", "no-store" );
        response.setHeader( "Pragma", "no-cache" );
        response.setDateHeader( "Expires", 0 );
        response.setContentType( "image/" + (sImgType.equalsIgnoreCase("png") ? "png" : "jpeg") );

        // Write the image to the client.
        ServletOutputStream outStream = response.getOutputStream();
        outStream.write( captchaBytes );
        outStream.flush();
        outStream.close();
    }

}
